use http::StatusCode;
use thiserror::Error;
use volo_http::response::ServerResponse;
use volo_http::server::IntoResponse;

#[allow(dead_code)]
#[derive(Error, Debug)]
pub enum AppError {
    #[error("error: `{0}`")]
    Anyhow(#[from] anyhow::Error),
    #[error("not found")]
    NotFound,
    #[error("bad request")]
    BadRequest,
}

impl IntoResponse for AppError {
    fn into_response(self) -> ServerResponse {
        match self {
            AppError::NotFound => (StatusCode::NOT_FOUND, self.to_string()).into_response(),
            AppError::BadRequest => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
            AppError::Anyhow(_) => {
                (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
            }
        }
    }
}
